<<<<<<< HEAD
class Solution
{
    vector<string> ret;
    string path;
    int left = 0;
    int right = 0;
    int n;
public:
    vector<string> generateParenthesis(int _n)
    {
        n = _n;
        dfs();
        return ret;
    }
    void dfs()
    {
        if (left > n || right > left) return;
        if (right == n)
        {
            ret.push_back(path);
            return;
        }

        left++;
        path.push_back('(');
        dfs();
        left--;
        path.pop_back();

        right++;
        path.push_back(')');
        dfs();
        right--;
        path.pop_back();
    }
};
class Solution
{
    int sum = 0;
    vector<int> path;
    vector<vector<int>> ret;
    int target;
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int t)
    {
        target = t;
        dfs(candidates, 0);
        return ret;
    }
    void dfs(vector<int>& candidates, int pos)
    {
        if (sum > target) return;
        if (sum == target) ret.push_back(path);
        for (int i = pos; i < candidates.size(); i++)
        {
            sum += candidates[i];
            path.push_back(candidates[i]);
            dfs(candidates, i);
            sum -= candidates[i];
            path.pop_back();
        }
    }
};
class Solution
{
    vector<string> hash = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    string path;
    vector<string> ret;
public:
    vector<string> letterCombinations(string digits)
    {
        if (digits.size() == 0) return {};
        dfs(digits, 0);
        return ret;
    }
    void dfs(string& str, int pos)
    {
        if (pos == str.size())
        {
            ret.push_back(path);
            return;
        }
        for (auto e : hash[str[pos] - '0'])
        {
            path.push_back(e);
            dfs(str, pos + 1);
            path.pop_back();
        }
    }
};
=======
#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool canFinish(int n, vector<vector<int>>& prerequisites)
    {
        unordered_map<int, vector<int>> edges;
        vector<int> in(n);
        for (auto e : prerequisites)
        {
            int x = e[0], y = e[1];
            edges[x].push_back(y);
            in[y]++;
        }
        queue<int> q;
        for (int i = 0; i < n; i++)
        {
            if (in[i] == 0) q.push(i);
        }
        while (q.size())
        {
            int front = q.front();
            q.pop();
            for (auto e : edges[front])
            {
                in[e]--;
                if (in[e] == 0) q.push(e);
            }
        }
        for (auto e : in)
        {
            if (e != 0) return false;
        }
        return true;
    }
};
class Trie
{
    struct Node
    {
        char ch;
        Node* next[26];
        bool isend;
    };
    Node* root;
public:
    Trie()
    {
        root = new Node();
    }

    void insert(string word)
    {
        Node* cur = root;
        for (int i = 0; i < word.size(); i++)
        {
            if (cur->next[word[i] - 'a'] == nullptr)
            {
                Node* newnode = new Node();
                newnode->ch = word[i];
                cur->next[word[i] - 'a'] = newnode;
            }
            cur = cur->next[word[i] - 'a'];
            if (i == word.size() - 1) cur->isend = true;
        }

    }

    bool search(string word)
    {
        Node* cur = root;
        for (int i = 0; i < word.size(); i++)
        {
            if (cur->next[word[i] - 'a'] == nullptr) return false;
            else
            {
                cur = cur->next[word[i] - 'a'];
                if (i == word.size() - 1 && cur->isend) return true;
                else if (i == word.size() - 1 && !cur->isend) return false;
            }
        }
        return true;
    }

    bool startsWith(string prefix)
    {
        Node* cur = root;
        for (int i = 0; i < prefix.size(); i++)
        {
            if (cur->next[prefix[i] - 'a'] == nullptr) return false;
            else
            {
                cur = cur->next[prefix[i] - 'a'];
                if (i == prefix.size() - 1) return true;
            }
        }
        return false;
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */
>>>>>>> 3f34cf1e6758b7dfbfaede1bb85481111b82d6b2
